#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#┃ General                                                                     ┃
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
set-option -g mouse on
set-option -g history-limit 1000000

# Needed for italic
set-option -g default-terminal "$TERM"

unbind C-b
set -g prefix M-v
bind 'M-v' send-prefix

set-option -s set-clipboard on
set-option -g renumber-windows on

# # Start windows and panes at 1, not 0
# set-option -g base-index      1
# set-option -g pane-base-index 1

set-option -g focus-events on
set-option -s escape-time 0    # Stop escape from sending Alt+
set-option -g wrap-search off

set-window-option -g mode-keys vi

#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#┃ Mappings                                                                    ┃
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
#useDrag1Pane
# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

bind-key -n 'C-h' if-shell $is_vim 'send-keys C-h'  'select-pane -L'
bind-key -n 'C-j' if-shell $is_vim 'send-keys C-j'  'select-pane -D'
bind-key -n 'C-k' if-shell $is_vim 'send-keys C-k'  'select-pane -U'
bind-key -n 'C-l' if-shell $is_vim 'send-keys C-l'  'select-pane -R'

bind-key -T copy-mode-vi 'C-h' select-pane -L
bind-key -T copy-mode-vi 'C-j' select-pane -D
bind-key -T copy-mode-vi 'C-k' select-pane -U
bind-key -T copy-mode-vi 'C-l' select-pane -R

bind-key -n WheelUpPane   if-shell $is_vim 'send-keys -M' copy-mode
bind-key -n WheelDownPane if-shell $is_vim 'send-keys -M' copy-mode

# Enter selection mode when highlghting with mouse
bind-key -n MouseDrag1Pane if-shell $is_vim 'send-keys -M' 'copy-mode -M'

# Keep selection highlighted when releasing drag
bind-key -n -T copy-mode-vi MouseDragEnd1Pane if-shell $is_vim 'send-keys -M' 'send-keys -X stop-selection'

bind-key -n              'M-u' copy-mode
bind-key -T copy-mode-vi 'M-u' send-keys -X halfpage-up
bind-key -T copy-mode-vi 'M-d' send-keys -X halfpage-down

bind-key -T copy-mode-vi 'M-n' send-keys -X next-prompt
bind-key -T copy-mode-vi 'M-p' send-keys -X previous-prompt

bind-key -n 'M-h' select-window -t :-
bind-key -n 'M-l' select-window -t :+
bind-key -n 'M-\' split-window -h    -c '#{pane_current_path}'
bind-key -n 'M-|' split-window -h -b -c '#{pane_current_path}'
bind-key -n 'M--' split-window       -c '#{pane_current_path}'
bind-key -n 'M-_' split-window    -b -c '#{pane_current_path}'
bind-key -n 'M-N' new-window

bind-key -n 'M-z' resize-pane -Z

bind-key -n 'M-H' resize-pane -L 10
bind-key -n 'M-L' resize-pane -R 10
bind-key -n 'M-K' resize-pane -U 10
bind-key -n 'M-J' resize-pane -D 10

# Same as default but removes confirmation
bind-key    'x'   kill-pane
bind-key    'M-x' kill-pane
bind-key -n 'M-X' kill-pane

bind-key -n 'M-C' send-keys "clear && tmux clear-history" \; send-keys "Enter"

# Use prefix + Ctrl+l/k to send sequence to shell
bind-key 'M-l' send-keys 'C-l' # Clear screen
bind-key 'M-k' send-keys 'C-k' # Delete right of cursor

bind-key -n 'M-R' command-prompt -I "#W" "rename-window -- '%%'"

bind-key -T copy-mode-vi y send -X copy-selection

#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#┃ Theme                                                                       ┃
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

cpu='#{cpu_fg_color}CPU:#{cpu_percentage}#[default]'
ram='#{ram_fg_color}RAM:#{ram_percentage}#[default]'

set -g status-justify               centre
set -g status-style                 fg=colour8
set -g status-left                  ' #{session_name} #{pane_index} #{?client_prefix,+,}'
set -g status-right                 " #{gh_notifications}  $cpu  $ram  %a %e %b  %H:%M "
set -g window-status-format         "  #{window_name} #{?window_zoomed_flag,, }#{?window_activity_flag,*, } "
set -g window-status-current-format "  #{window_name} #{?window_zoomed_flag,, }#{?window_activity_flag,*, } "
set -g window-status-current-style  fg=colour7,bold
set -g window-status-separator      ''
set -g pane-border-style            fg=colour8
set -g pane-border-status           top
set -g pane-border-format           "#{pane_title}"
set -g message-style                bg=colour8,fg=colour7
set -g pane-active-border-style     fg=colour8
set -g mode-style                   bg=colour8

# set-hook -g after-split-window 'select-pane -T ""'
# set-hook -g after-new-window   'select-pane -T ""'
# set-hook -g after-new-session  'select-pane -T ""'

# set-window-option -g visual-bell off
# set-window-option -g monitor-bell     on
# set-window-option -g visual-bell      off
# set-window-option -g bell-action      other

# set-window-option -g monitor-activity off
# set-window-option -g activity-action  other
# set-window-option -g visual-activity  off

bind-key -n M-q display-menu -x W -y S \
    "Horizontal Split"                   | "split-window -h" \
    "Vertical Split"                     - "split-window -v"  \
    "Swap Up"                            u "swap-pane -U" \
    "Swap Down"                          d "swap-pane -D" \
    "Clear pane"                         c "send-keys \"clear && tmux clear-history\" \; send-keys \"Enter\"" \
    "Kill Pane"                          x kill-pane \
    "New Window"                         N new-window \
    "Rename Window"                      R "command-prompt -I \"#W\" \"rename-window -- '%%'\"" \
    "#{?window_zoomed_flag,Unzoom,Zoom}" z "resize-pane -Z"

bind-key -n M-/ display-menu -x W -y S -T '#[fg=colour4]Readline Bindings' \
    "#[fg=colour1]Moving" "" "" "" \
    "Move to start of current line"           C-a "send-keys C-a" \
    "Move to end of current line"             C-e "send-keys C-e" \
    "Move forward a character"                C-f "send-keys C-f" \
    "Move backward a character"               C-b "send-keys C-b" \
    "Move forward to end of next word"        M-f "send-keys M-f" \
    "Move backward to start of previous word" M-b "send-keys M-b" \
    "" "#[fg=colour1]Changing Text" "" "" "" \
    "Delete the character under the cursor"   C-d "send-keys C-d" \
    "Transpose words"                         M-t "send-keys M-t" \
    "" "#[fg=colour1]Cutting and Pasting" "" "" "" \
    "Kill (cut) forwards to the end of the line"                                   C-k  "send-keys C-k" \
    "Kill (cut) backwards to the start of the line"                                C-u  "send-keys C-u" \
    "^"                                                                            C-x  "send-keys C-x" \
    "Kill (cut) forwards to the end of the current word"                           M-d  "send-keys M-d" \
    "Kill (cut) backwards to the start of the current word"                        C-w  "send-keys C-w" \
    "Delete whitespace around the cursor"                                          M-\\ "send-keys M-\\" \
    "Yank (paste) the top of the kill ring"                                        C-y  "send-keys C-y" \
    "Rotate the kill ring, and yank (paste) the new top. Only works after a yank"  M-y  "send-keys M-y" \
    "" "#[fg=colour1]History" "" "" "" \
    "Fetch the previous command from the history list"    C-p   "send-keys C-p" \
    "Fetch the next command from the history list"        C-n   "send-keys C-n" \
    "Move to the first line in the history"               M-<   "send-keys M-<" \
    "Move to the last line in the history"                M->   "send-keys M->" \
    "Search backward through history"                     C-r   "send-keys C-r" \
    "Search forward through history"                      C-s   "send-keys C-s" \
    "Search backward through history for a given string"  M-p   "send-keys M-p" \
    "Search forward through history for a given string"   M-n   "send-keys M-n" \
    "Insert the first argument to the previous command"   C-M-y "send-keys C-M-y" \
    "Insert the last argument to the previous command"    M-_   "send-keys M-_" \
    "^"                                                   M-.   "send-keys M-."

# Plugins

set -g @plugin 'lewis6991/gh_notify.tmux'
set -g @plugin 'tmux-plugins/tmux-cpu'

if "test ! -d ~/.tmux/plugins/tpm" \
   "run 'git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm && ~/.tmux/plugins/tpm/bin/install_plugins'"

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
